/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorDrawRectanglePlugin = Class.create(FNISVGEditorPlugin, {
  initialize : function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);

    var button = new FNIToggleableButton( {});
    button.domNode.addClassName('fniSVGEditor_DrawRectPlugin_Button');
    button.setImage(this.getPluginURL() + "/gfx/icons/22x22/tools/draw-rectangle.png");
    button.addListener('buttonUp', this, this._onButtonUp);
    button.addListener('buttonDown', this, this._onButtonDown);
    button.setTooltip(SVGLocale.getText("drawrectPlugin_buttonTooltip"));
    editorInstance.registerToolbarItem('drawRect', button);
    
    this._onKeyPressListener = this._onKeyPress.bindAsEventListener(this);
    Event.observe(window, "keydown", this._onKeyPressListener);
    this._drawing = false;
  },
  getName : function() {
    return "drawrect";
  },
  _onButtonUp : function(event) {
    this._stopListening();
  },
  _onButtonDown : function(event) {
    this._startListening();
  },
  _startListening : function() {
    this.getEditorInstance().addListener("mousedragstart", this, this._onEditorMouseDragStart);
    this.getEditorInstance().addListener("mousedragend", this, this._onEditorMouseDragEnd);
    this.getEditorInstance().addListener("mousedragging", this, this._onEditorMouseDragging);
  },
  _stopListening : function() {
    this.getEditorInstance().removeListener("mousedragstart", this);
    this.getEditorInstance().removeListener("mousedragend", this);
    this.getEditorInstance().removeListener("mousedragging", this);
  },
  _onEditorMouseDragStart : function(event) {
    this._dragElement = this.getEditorInstance().getSVGDocument().createElement("rect");
    this._dragElement.setAttribute('style', 'stroke-dasharray:1,1; fill:none; fill-opacity: 0; stroke:#000000;stroke-width:1;stroke-opacity:1');
    this.getEditorInstance().getSVGDocument().appendTemporaryElement(this._dragElement);
    var controller = getSVGElementControllerVault().getElementControllerFor(this._dragElement);
    controller.moveTo(this._dragElement, event.mouseEditorX, event.mouseEditorY);
    this._drawing = true;
    this.getEditorInstance().getSelectionManager().setMode(FNISelectionManager.MODE_NONE);
  },
  _onEditorMouseDragEnd : function(event) {
    if (this._drawing == true) {
      var rPos = this._dragElement.getBBox();

      var rectElement = this.getEditorInstance().createSVGElement("rect");
      var controller = getSVGElementControllerVault().getElementControllerFor(rectElement);

      controller.moveTo(rectElement, rPos.x, rPos.y);
      controller.resizeTo(rectElement, rPos.width, rPos.height);

      this.getEditorInstance().appendSVGElement(rectElement);
      this._editorInstance.getUndoManager().addUndoableEvent(new FNISVGEditorUndoableElementAddEvent(rectElement));
      this._stopDrawing();
    }
  },
  _onEditorMouseDragging : function(event) {
    if (this._drawing == true) {
      var elementController = getSVGElementControllerVault().getElementControllerFor(this._dragElement);

      var width = event.mouseEditorX - event.dragStartX;
      var height = event.mouseEditorY - event.dragStartY;
      var x = event.dragStartX;
      var y = event.dragStartY;

      if (width < 0) {
        width = -width;
        x -= width;
      }

      if (height < 0) {
        height = -height;
        y -= height;
      }

      if (event.ctrlKey) {
        /** TODO: **/
      }

      if (event.shiftKey) {
        /** TODO: **/
      }

      elementController.resizeTo(this._dragElement, width, height);
      elementController.moveTo(this._dragElement, x, y);
    }
  },
  _onKeyPress : function(event) {
    if (event.keyCode == Event.KEY_ESC) {
      this._stopDrawing();
    }
  },
  _stopDrawing : function() {
    this.getEditorInstance().getSVGDocument().removeTemporaryElement(this._dragElement);
    this._dragElement = null;
    this._drawing = false;
    this.getEditorInstance().getSelectionManager().setPreviousMode();
  }
});

document.fire("svgEditorPluginLoaded:drawrect", {
  pluginClass : FNISVGEditorDrawRectanglePlugin
});
